package com.sanji.sjmallmodule.goods;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.MallApplication;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.datamanager.SelectDataManager;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallmodule.login.RegisterRegionAdpter;
import com.sanji.sjmallmodule.login.RegisterRegionBean;
import com.sanji.sjmallutils.Tools;
import com.sanji.sjmallwidget.XListView;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*****
 * 商品筛选
 ***/
public class SelectTypeActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, NoHttpListener, XListView.IXListViewListener {
    @BindView(R.id.select_type_view)
    View selectTypeView;
    @BindView(R.id.select_first_arrow)
    CheckBox brandArrow;
    @BindView(R.id.select_second_arrow)
    CheckBox priceArrow;
    @BindView(R.id.select_third_arrow)
    CheckBox ramArrow;
    @BindView(R.id.select_type_list)
    XListView typeListView;
    @BindView(R.id.select_type_checkbox)
    CheckBox selectTypeCheckBox;//筛选按钮
    RelativeLayout inputLinear;
    private String searchType;
    private TextView commonLeftTv;
    private PopupWindow brandPop;//品牌pop
    private PopupWindow pricePop;//价格pop
    private PopupWindow ramPop;//运行内存pop
    //筛选pw
    private PopupWindow listPWindow;
    private ListView listViewPop;
    private RegisterRegionAdpter listPwAdapter;
    private List<RegisterRegionBean> listPwData;
    //
    private Button popBtn;
    private GridView brandGridView;
    private GridView priceGridView;
    private GridView ramGridView;

    private SearchTypeAdapter brandAdapter;
    private SearchTypeAdapter priceAdapter;
    private SearchTypeAdapter ramAdapter;
    private SearchTypeAdapter partBrandAdapter;

    private List<SearchTypeBean> brandData;
    private List<SearchTypeBean> partBrandData;//配件品牌接口
    private List<SearchTypeBean> priceData;
    private List<SearchTypeBean> ramData;
    private SearchGoodsAdapter goodsAdapter;
    private List<GoodsBean> goodsData;
    private int flag = 0;
    private String intentType = "";
    //品牌的参数
    private String brandParam = "";

    //运行内存参数
    private String ramParam = "";
    //综合排序
    private String sortFieldParam = "";
    private String sortDirectionParam = "";
    //筛选的最后参数
    private String finalUrl = "";

    private Request<JSONObject> request;

    private boolean pagerCanloder = false;
    private boolean refresh = false;
    private String userId = "";//用户的userId
    private String userIdParam = "?userId=";//用户userId参数
    private String pageSizerParam = "&rows=10";//每页行数
    private String goodsTypeParam = " ";//品类参数
    private String machineParam = "";//机型参数 brandIds
    private String catIdsParam = "";//类型id参数
    private int page = 1;//页数
    private String searchParam = "";//模糊查询字段
    private String minPriceParam = "";//最小值参数
    private String maxPriceParam = "";//最大值参数
    private Button selectOk;//筛选确认按钮
    private EditText minEdt;
    private EditText maxEdt;
    private String minStr = "";
    private String maxStr = "";
    private int position;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_type);
        ButterKnife.bind(this);
        initView();
        itemOnclick();
    }

    public void initView() {
        commonLeftTv = (TextView) findViewById(R.id.common_left_tv);
        findViewById(R.id.common_back_img).setOnClickListener(this);
        userId = myShare.getString(ConfigConts.USER_ID, null);
        intentType = getIntent().getStringExtra("intentType");
        position = getIntent().getIntExtra("selectI", 0);
        if (intentType.equals(ConfigConts.MACHINE_PHONE)) {
            String searchName = getIntent().getStringExtra("searchName");
            commonLeftTv.setText(searchName);
            brandArrow.setText(searchName);
            brandParam = "&brandIds=" + getIntent().getStringExtra("brandIds");
            goodsTypeParam = "sj";
            machineParam = "&machine_type=" + ConfigConts.MACHINE_PHONE;
            finalUrl = getFinalUrl();
        } else if (intentType.equals(ConfigConts.MACHINE_TABLE)) {
            String searchName = getIntent().getStringExtra("searchName");
            commonLeftTv.setText(searchName);
            brandArrow.setText(searchName);
            goodsTypeParam = "sj";
            brandParam = "&brandIds=" + getIntent().getStringExtra("brandIds");
            machineParam = "&machine_type=" + ConfigConts.MACHINE_TABLE;
            finalUrl = getFinalUrl();
        } else if (intentType.equals(ConfigConts.MACHINE_PARET)) {
            //配件过来的，修改筛选条件

            ramArrow.setVisibility(View.GONE);
            String searchName = getIntent().getStringExtra("searchName");

            commonLeftTv.setText(searchName);
            priceArrow.setText(searchName);
            brandArrow.setText("品牌");
            goodsTypeParam = "accessory";
            catIdsParam = "&catIds=" + getIntent().getStringExtra("catIds");
            finalUrl = getFinalUrl();
        } else if (intentType.equals(ConfigConts.MACHINE_LIFE)) {
            String searchName = getIntent().getStringExtra("searchName");
            commonLeftTv.setText(searchName);
            brandArrow.setText(searchName);
            goodsTypeParam = "sj";
            brandParam = "&brandIds=" + getIntent().getStringExtra("brandIds");
            machineParam = "&machine_type=" + ConfigConts.MACHINE_LIFE;
            finalUrl = getFinalUrl();
        } else if (intentType.equals(ConfigConts.MACHINE_STORE)) {
            String searchName = getIntent().getStringExtra("searchName");
            commonLeftTv.setText(searchName);
            brandArrow.setText(searchName);
            goodsTypeParam = "sj";
            brandParam = "&brandIds=" + getIntent().getStringExtra("brandIds");
            machineParam = "&machine_type=" + ConfigConts.MACHINE_STORE;
            finalUrl = getFinalUrl();
        } else if (intentType.equals(ConfigConts.MULTIPLE_PART)) {
            //
            priceArrow.setText("类型");
            ramArrow.setVisibility(View.GONE);
            String searchName = getIntent().getStringExtra("selectParam");
            goodsTypeParam = "accessory";
            commonLeftTv.setText(searchName);
            searchParam = "&searchParam=" + URLEncoder.encode(searchName);
            finalUrl = getFinalUrl();
        } else if (intentType.equals(ConfigConts.MULTIPLE_PHONE)) {
            String searchName = getIntent().getStringExtra("selectParam");
            goodsTypeParam = "sj";
            commonLeftTv.setText(searchName);
            searchParam = "&searchParam=" + URLEncoder.encode(searchName);
            finalUrl = getFinalUrl();
        }
        brandData = new ArrayList<>();
        priceData = new ArrayList<>();
        ramData = new ArrayList<>();
        goodsData = new ArrayList<>();
        listPwData = new ArrayList<>();

        listPwAdapter = new RegisterRegionAdpter(listPwData, this);
        partBrandData = new ArrayList<>();

        ramData = SelectDataManager.getRamRange();

        goodsAdapter = new SearchGoodsAdapter(this, goodsData);
        typeListView.setAdapter(goodsAdapter);
        brandArrow.setOnCheckedChangeListener(this);
        priceArrow.setOnCheckedChangeListener(this);
        ramArrow.setOnCheckedChangeListener(this);
        selectTypeCheckBox.setOnCheckedChangeListener(this);
        typeListView.setPullLoadEnable(false);
        typeListView.setHeaderDividersEnabled(false);
        typeListView.setFooterDividersEnabled(false);
        typeListView.setXListViewListener(this);
        //网络发送请求
        refresh = true;
        requestPhoneData();
    }


    /**
     * showBrandPopView 的显示
     */

    public void showBrandPopWindow() {
        //弹出popwindow
        backgroundAlpaha(this, 0.6f);
        if (null == brandPop) {
            View view = LayoutInflater.from(this).inflate(R.layout.activity_select_type_popwindow, null);
            inputLinear = (RelativeLayout) view.findViewById(R.id.price_relative);
            inputLinear.setVisibility(View.GONE);
            brandGridView = (GridView) view.findViewById(R.id.filter_type_gridview);
            brandGridView.setNumColumns(2);
            popBtn = (Button) view.findViewById(R.id.select_ok_btn);
            brandPop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            brandPop.setBackgroundDrawable(new ColorDrawable());
            brandPop.setFocusable(true);
            brandPop.setOutsideTouchable(true);
            brandPop.setAnimationStyle(R.style.my_popwindow_left_anim_style);
        }
        brandPop.showAsDropDown(selectTypeView);
        brandPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpaha(SelectTypeActivity.this, 1.0f);
                showViewPop(flag);
            }
        });
        if (intentType.equals(ConfigConts.MACHINE_PHONE)) {
            brandData = MallApplication.getApplication().getPhoneList();

        } else if (intentType.equals(ConfigConts.MACHINE_TABLE)) {
            brandData = MallApplication.getApplication().getTableList();

        } else if (intentType.equals(ConfigConts.MACHINE_PARET)) {
            brandData = MallApplication.getApplication().getPartBrandList();
        } else if (intentType.equals(ConfigConts.MACHINE_LIFE)) {
            brandData = MallApplication.getApplication().getSmartLifeList();

        } else if (intentType.equals(ConfigConts.MACHINE_STORE)) {

            brandData = MallApplication.getApplication().getSmartStoreList();

        } else if (intentType.equals(ConfigConts.MULTIPLE_PART)) {
            //综合查找配件的请求
            brandData = MallApplication.getApplication().getPartList();

        } else if (intentType.equals(ConfigConts.MULTIPLE_PHONE)) {
            //综合查找非配件的请求
            brandData = MallApplication.getApplication().getAllNoPhoneList();
        }
        if (null == brandAdapter) {
            brandAdapter = new SearchTypeAdapter(mCtx, brandData);
            if (!intentType.equals(ConfigConts.MACHINE_PARET)) {
                brandAdapter.setSelection(position);
            }
            brandGridView.setAdapter(brandAdapter);
        } else {
            brandAdapter.notifyAdapter(brandData);
        }

        setRadioGroup(brandGridView, brandAdapter, brandData);

        popBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                brandPop.dismiss();
                finalUrl = getFinalUrl();
                refresh = true;
                page = 1;
                requestPhoneData();
            }
        });
    }

    /**
     * showPricePopView 的显示
     */
    public void showPricePopWindow() {
        //弹出popwindow
        backgroundAlpaha(this, 0.6f);
        if (null == pricePop) {
            View view = LayoutInflater.from(this).inflate(R.layout.activity_select_type_popwindow, null);
            selectOk = (Button) view.findViewById(R.id.select_ok_btn);
            minEdt = (EditText) view.findViewById(R.id.min_price_edt);
            maxEdt = (EditText) view.findViewById(R.id.max_price_edt);
            inputLinear = (RelativeLayout) view.findViewById(R.id.price_relative);
            inputLinear.setVisibility(View.VISIBLE);
            priceGridView = (GridView) view.findViewById(R.id.filter_type_gridview);
            priceGridView.setNumColumns(3);
            pricePop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            pricePop.setBackgroundDrawable(new ColorDrawable());
            pricePop.setFocusable(true);
            pricePop.setOutsideTouchable(true);
            pricePop.setAnimationStyle(R.style.my_popwindow_left_anim_style);
        }

        pricePop.showAsDropDown(selectTypeView);
        pricePop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpaha(SelectTypeActivity.this, 1.0f);
                showViewPop(flag);
            }
        });

        if (intentType.equals(ConfigConts.MACHINE_PARET) || intentType.equals(ConfigConts.MULTIPLE_PART)) {
            //配件的类型
            priceData = MallApplication.application.getPartList();
            inputLinear.setVisibility(View.GONE);
        } else {
            priceData = SelectDataManager.getPriceRange();
        }
        if (null == priceAdapter) {
            priceAdapter = new SearchTypeAdapter(mCtx, priceData);
            if (intentType.equals(ConfigConts.MACHINE_PARET)) {
                priceAdapter.setSelection(position);
            }
            priceGridView.setAdapter(priceAdapter);
        } else {
            priceAdapter.notifyAdapter(priceData);
        }
//        addRadioButton(priceData);
        setRadioGroup(priceGridView, priceAdapter, priceData);
        selectOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pricePop.dismiss();
                minStr = minEdt.getText().toString().trim();
                maxStr = maxEdt.getText().toString().trim();
                if (!minStr.equals("")) {
                    minPriceParam = "&minPrice=" + minStr;
                    if (maxStr.equals("")) {
                        maxPriceParam = "&maxPrice=100000";
                        priceArrow.setText(minStr + "以上");
                    } else {
                        maxPriceParam = "&maxPrice=" + maxStr;
                        priceArrow.setText(minStr + "-" + maxStr);
                    }
                }
                finalUrl = getFinalUrl();
                refresh = true;
                page = 1;
                requestPhoneData();
            }
        });
    }

    /**
     * showRamPopView 的显示
     */
    public void showRamPopWindow() {
        //弹出popwindow
        backgroundAlpaha(this, 0.6f);
        if (null == ramPop) {
            View view = LayoutInflater.from(this).inflate(R.layout.activity_select_type_popwindow, null);
            selectOk = (Button) view.findViewById(R.id.select_ok_btn);
            inputLinear = (RelativeLayout) view.findViewById(R.id.price_relative);
            inputLinear.setVisibility(View.GONE);
            ramGridView = (GridView) view.findViewById(R.id.filter_type_gridview);
            ramGridView.setNumColumns(3);
            ramPop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            ramPop.setBackgroundDrawable(new ColorDrawable());
            ramPop.setFocusable(true);
            ramPop.setOutsideTouchable(true);
            ramPop.setAnimationStyle(R.style.my_popwindow_left_anim_style);
            if (null == ramAdapter) {
                ramAdapter = new SearchTypeAdapter(mCtx, ramData);
                ramGridView.setAdapter(ramAdapter);
            } else {
                ramAdapter.notifyAdapter(ramData);
            }
        }

        ramPop.showAsDropDown(selectTypeView);
        ramPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpaha(SelectTypeActivity.this, 1.0f);
                showViewPop(flag);
            }
        });
        setRadioGroup(ramGridView, ramAdapter, ramData);
        selectOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ramPop.dismiss();
                finalUrl = getFinalUrl();

                refresh = true;
                page = 1;
                requestPhoneData();
            }
        });

    }

    /**
     * 点击筛选以后弹出listView
     */
    public void showListPw() {
        backgroundAlpaha(this, 0.6f);
        if (listPWindow == null) {
            View view = LayoutInflater.from(this).inflate(R.layout.activity_select_type_xlist_popwindow, null);
            listViewPop = (ListView) view.findViewById(R.id.select_type_list_pop);
            listPWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            listPWindow.setBackgroundDrawable(new ColorDrawable());

            listPWindow.setFocusable(true);
            listPWindow.setOutsideTouchable(true);
            //增加数据
            listPwData = SelectDataManager.getSelectFilter();
            listPwAdapter.notifyAdapter(listPwData);
            listViewPop.setAdapter(listPwAdapter);
            listPWindow.setAnimationStyle(R.style.my_popwindow_right_anim_style);

            listPWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    backgroundAlpaha(SelectTypeActivity.this, 1.0f);
                    selectTypeCheckBox.setChecked(false);
                }
            });
            listViewPop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    RegisterRegionBean bean = (RegisterRegionBean) listPwAdapter.getItem(i);
                    selectTypeCheckBox.setText(bean.getRegionName());
                    listPWindow.dismiss();
                    selectTypeCheckBox.setChecked(false);
                    switch (i) {
                        case 0:
                            //价格从低到高
                            sortFieldParam = "&sort=price";
                            sortDirectionParam = "&orders=asc";
                            break;
                        case 1:
                            //价格从高到低
                            sortFieldParam = "&sort=price";
                            sortDirectionParam = "&orders=desc";
                            break;
                        case 2:
                            //新品优先
                            sortFieldParam = "&sort=create_Time";
                            sortDirectionParam = "&orders=desc";
                            break;
                    }
                    //发送请求
                    refresh = true;
                    page = 1;
                    finalUrl = getFinalUrl();
                    requestPhoneData();
                }
            });

        }
        listPWindow.showAsDropDown(selectTypeView);
    }

    /**
     * 设置添加屏幕的背景透明度
     **/
    public void backgroundAlpaha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow()
                .addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    /***
     * popwindow的显示与与图标的形状改变
     */
    private void showViewPop(int flag) {
        switch (flag) {
            case 1:
                setViewPop(brandArrow, brandPop);
                break;
            case 2:
                setViewPop(priceArrow, pricePop);
                break;
            case 3:
                setViewPop(ramArrow, ramPop);
                break;
        }
    }

    public void setViewPop(CheckBox box, PopupWindow pop) {
        if (pop.isShowing()) {
            box.setChecked(true);
        } else {
            box.setChecked(false);
        }
    }

    /**
     * 对radioGrop进行监听
     */
    public void setRadioGroup(final GridView popGridView, final SearchTypeAdapter adapter, final List<SearchTypeBean> mData) {
        popGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SearchTypeBean bean = (SearchTypeBean) adapter.getItem(i);
                adapter.setSelection(i);
                adapter.notifyAdapter(mData);
                switch (flag) {
                    case 1:
                        //品牌
                        brandParam = "";
                        brandParam = "&brandIds=" + bean.getId();
                        brandArrow.setText(bean.getSearchTypeName());
                        brandArrow.setTextColor(getResources().getColor(R.color.colorRed));
                        commonLeftTv.setText(bean.getSearchTypeName());
                        break;
                    case 2:
                        if (intentType.equals(ConfigConts.MACHINE_PARET) || intentType.equals(ConfigConts.MULTIPLE_PART)) {
                            //类型s
                            catIdsParam = "";
                            catIdsParam = "&catIds=" + bean.getId();
                        } else {
                            //价格
                            minPriceParam = "";
                            maxPriceParam = "";
                            if (bean.getSearchTypeName().equals(getString(R.string.above_4500))) {
                                minPriceParam = "&minPrice=4500";
                                maxPriceParam = "&maxPrice=100000";
                            } else {
                                String[] price = Tools.separeteStr(bean.getSearchTypeName());
                                minPriceParam = "&minPrice=" + price[0];
                                maxPriceParam = "&maxPrice=" + price[1];
                            }

                        }
                        priceArrow.setTextColor(getResources().getColor(R.color.colorRed));
                        priceArrow.setText(bean.getSearchTypeName());
                        break;
                    case 3:
                        //运行内存
                        ramParam = "";
                        if (bean.getSearchTypeName().equals(getString(R.string.no_set))) {
                            ramParam = "";
                            ramArrow.setTextColor(getResources().getColor(R.color.color_4f4f4f
                            ));
                            ramArrow.setText(getString(R.string.ram));
                        } else {
                            ramParam = "&rams=" + bean.getSearchTypeName();
                            ramArrow.setTextColor(getResources().getColor(R.color.colorRed));
                            ramArrow.setText(bean.getSearchTypeName());
                        }

                        break;
                }
            }
        });
    }

    /**
     * 点击确认后，请求选择数据
     */
    public void requestPhoneData() {
        request = NoHttp.createJsonObjectRequest(finalUrl, RequestMethod.GET);
        request.setCancelSign(finalUrl.hashCode());
        NoHttpCallBack.getInstance().add(mCtx, finalUrl.hashCode(), request, this, true, true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()) {
            case R.id.select_first_arrow:
                flag = 1;
                //品牌
                if (b) {
                    showBrandPopWindow();
                } else {
                    if (null != brandPop) {
                        brandPop.dismiss();
                    }
                }
                break;
            case R.id.select_second_arrow:
                //价格
                flag = 2;
                //增加价格数据
                if (b) {
                    showPricePopWindow();
                } else {
                    if (null != pricePop) {
                        pricePop.dismiss();
                    }
                }
                break;
            case R.id.select_third_arrow:
                //内存
                flag = 3;
                if (b) {
                    showRamPopWindow();
                } else {
                    if (null != ramPop) {
                        ramPop.dismiss();
                    }
                }
                break;
            case R.id.select_type_checkbox:
                if (b) {
                    showListPw();
                } else {
                    if (null != listPWindow) {
                        listPWindow.dismiss();
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 点击listItem
     */
    public void itemOnclick() {
        typeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                GoodsBean phone = (GoodsBean) goodsAdapter.getItem(i - 1);
                intent = new Intent(SelectTypeActivity.this, GoodsDetailsActivity.class);
                intent.putExtra("goodsCode", phone.getGoodsCode());
                intent.putExtra("goodsType", goodsTypeParam);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onSuccessed(int what, Response response) {
        JSONObject object = (JSONObject) response.get();
        String status = object.optString(ConfigConts.STATUS_STRING);
        if (status.equals(ConfigConts.STATUS_SUCCESS)) {
            if (what == finalUrl.hashCode()) {
                JSONObject json = object.optJSONObject("result");
                JSONArray array = json.optJSONArray("goodsList");
                int totalPages = json.optInt("tatalPage");
                if (totalPages > page) {
                    pagerCanloder = true;
                } else {
                    pagerCanloder = false;
                }
                typeListView.setPullLoadEnable(pagerCanloder);
                if (refresh) {
                    goodsData.clear();
                    refresh = false;
                }
                if (array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        GoodsBean goodsBean = new GoodsBean();
                        try {
                            JSONObject jsonObject = (JSONObject) array.get(i);
                            goodsBean.setUrl(jsonObject.optString("DEFAULT_IMG"));
                            goodsBean.setPhoneName(jsonObject.optString("GOODS_NAME"));
                            Object priceObj = jsonObject.get("PRICE");
                            goodsBean.setPhonePrice(Double.valueOf(priceObj.toString()));
                            goodsBean.setGoodsCode(jsonObject.optString("GOODS_NUM"));
                            goodsBean.setSkuId(jsonObject.optString("ID"));
                            goodsBean.setStock(jsonObject.optInt("STOCK"));
                            goodsData.add(goodsBean);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    Tools.toastTools(mCtx, getString(R.string.goods_no_detail_message));
                }
                goodsAdapter.notifyAdapter(goodsData);
                typeListView.stopRefresh();
                typeListView.stopLoadMore();
            }
        } else {
            String errorMs = object.optString(ConfigConts.ERRORMSG);
            Tools.toastTools(mCtx, errorMs);
        }
    }

    @Override
    public void onFailed(int what, Response response) {
        Tools.toastTools(mCtx, getString(R.string.false_message));
    }

    @Override
    public void onRefresh() {
        //刷新
        refresh = true;
        page = 1;
        finalUrl = getFinalUrl();
        requestPhoneData();
    }

    @Override
    public void onLoadMore() {
        //加载更多
        if (pagerCanloder) {
            page++;
            finalUrl = getFinalUrl();
            requestPhoneData();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (request != null) {
            request.cancel();
        }
    }

    /**
     * @return 最后的url
     */
    public String getFinalUrl() {
        String url = ConfigAPI.GOODS_SEARCH_URL + goodsTypeParam + userIdParam + userId + catIdsParam + pageSizerParam + "&page="
                + page + sortFieldParam + sortDirectionParam + maxPriceParam + minPriceParam + brandParam + searchParam + ramParam + machineParam;
        return url;
    }
}
